"use strict";

const Controller = require("egg").Controller;

class ProductImageController extends Controller {
  /**
   * 获取商品图片列表
   */
  async index() {
    const { ctx } = this;
    const { productId } = ctx.query;

    const images = await ctx.service.productImage.getListByProductId(productId);

    ctx.body = {
      code: 0,
      message: "success",
      data: images,
    };
  }

  /**
   * 创建商品图片
   */
  async create() {
    const { ctx } = this;
    const { productId, images } = ctx.request.body;

    const result = await ctx.service.productImage.create(productId, images);

    ctx.body = {
      code: 0,
      message: "创建成功",
      data: result,
    };
  }

  /**
   * 更新商品图片
   */
  async update() {
    const { ctx } = this;
    const { id } = ctx.params;
    const { image, sort_order, is_cover } = ctx.request.body;

    const result = await ctx.service.productImage.update(id, {
      image,
      sort_order,
      is_cover,
    });

    ctx.body = {
      code: 0,
      message: "更新成功",
      data: result,
    };
  }

  /**
   * 删除商品图片
   */
  async destroy() {
    const { ctx } = this;
    const { id } = ctx.params;

    const result = await ctx.service.productImage.delete(id);

    ctx.body = {
      code: 0,
      message: "删除成功",
      data: result,
    };
  }
}

module.exports = ProductImageController;
